package com.sbs.gaoxinqutoilet.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.sbs.gaoxinqutoilet.activity.LoginActivity;
import com.sbs.gaoxinqutoilet.entity.UserInforEntity;
import com.sbs.gaoxinqutoilet.eventbus.EventCenter;
import com.sbs.gaoxinqutoilet.http.Request;
import com.sbs.gaoxinqutoilet.interfaces.Constant;
import com.sbs.gaoxinqutoilet.interfaces.EventConstant;
import com.sbs.gaoxinqutoilet.utils.GsonTools;
import com.sbs.gaoxinqutoilet.utils.LogUtils;
import com.sbs.gaoxinqutoilet.utils.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;

/**
 * If you change her, tell me
 * sometimes I believe compiler ignores all my comments
 * Created by gyyak46 on 2017/8/18.
 */

public class LoginLinkService extends Service{

    private static final int LOGIN_INDEX = 01;
    private Timer mTimer;

    private long startTime = 20 * 60 * 1000;
    private long sendTime = 20 * 60 * 1000;

//    private Handler handler=new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            if (msg.what == LOGIN_INDEX) {
//                requestLoginUser();
//            }
//        }
//    };


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        super.onCreate();
       // mTimer = new Timer();
       // setTimerTask();
        EventBus.getDefault().register(this);
    }


//    private void setTimerTask() {
//        mTimer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                handler.sendEmptyMessage(LOGIN_INDEX);
//                Log.e("显示", "执行登录");
//            }
//        }, startTime, sendTime);
////        }, 30000, 50000);
//
//    }

    //用户登录
    private void requestLoginUser() {

        //重新登录
        intentLoginActivity();


//        String username = SPUtil.getString(Constant.KEY_USERNAME, null);
//        String pwd = SPUtil.getString(Constant.KEY_USERPWD, null);
//        String imei = SPUtil.getString(Constant.KEY_IMEI, null);
//        if (username != null && pwd != null && imei != null) {
//            // otp 使用固定的  E6B503D04A3FC580047CA334FF5112C9
//            String loginUrl = HttpApi.LOGIN_URL + "name=" + username + "&password=" + pwd + "&imei=" + imei+"&otp="+"E6B503D04A3FC580047CA334FF5112C9";
//            HttpUtils.requestGet(this, loginUrl, new LoginRequest());
//        }

    }


    private class LoginRequest extends Request {
        @Override
        public void success(String jsonStr) {
            super.success(jsonStr);
            LogUtils.e("jsonStrLogin", jsonStr);
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(jsonStr);
                int success = jsonObject.optInt("success");
               // success=0;
                if (success == 1) {  //登陆成功

                    UserInforEntity userEntity = GsonTools.changeGsonToBean(jsonStr, UserInforEntity.class);
                    if (userEntity.getIsvalid().equals(Constant.WEIZHI)) {
                        // 如果isvalid 是1 说明进行重置过 需要重新登录
                        SPUtil.putString(Constant.KEY_ONELOGIN,"");
                        intentLoginActivity();
                    }
                }else{
                    //重新登录
                    intentLoginActivity();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void error(String error) {
            super.error(error);
        }
    }

    private void intentLoginActivity() {
        //重新登录
        SPUtil.remove(Constant.KEY_USERNAME);
        SPUtil.remove(Constant.KEY_USERPWD);
        SPUtil.remove(Constant.KEY_IMEI);
        Intent intent = new Intent();
        intent.setClass(LoginLinkService.this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

        //关闭 mianActivity
         EventBus.getDefault().post(new EventCenter(EventConstant.FINISH_MAIN_AC));

    }

    @Subscribe
    public void onReceiverEvent(EventCenter eventCenter){
          //接受开屏发送的广播
        if (eventCenter.getEventCode()== EventConstant.LOGIN_LINK) {
            LogUtils.e("显示","接受广播"+eventCenter.getEventCode());
            requestLoginUser();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        //mTimer.cancel();
        //handler.removeCallbacksAndMessages(null);
    }
}
